package com.burgeon.backend.EnterpriseWeChat.service.Impl;

import com.burgeon.backend.EnterpriseWeChat.entity.BirthdayVip;
import com.burgeon.backend.EnterpriseWeChat.entity.Vip;
import com.burgeon.backend.EnterpriseWeChat.rest.Gtasks.GetFeedBack;
import com.burgeon.backend.EnterpriseWeChat.rest.Gtasks.GetStoredValueVip;
import com.burgeon.backend.EnterpriseWeChat.rest.Gtasks.GetStoredValueVipCount;
import com.burgeon.backend.EnterpriseWeChat.service.EWStoredValueService;
import com.burgeon.backend.EnterpriseWeChat.service.GtasksService;
import com.burgeon.backend.entity.Info;
import com.burgeon.backend.mappers.AccountMapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;

@Service
public class EWStoredValueServiceImpl implements EWStoredValueService {
    private final static Log logger = LogFactory.getLog(GtasksService.class);
    @Autowired
    private AccountMapper accountMapper;

    private Info GetInfoByToken(String token) {
        return accountMapper.GetTokenData(token);
    }

    /**
     * 储值邀约
     * @param token
     * @param empId
     * @param type
     * @param page
     * @return
     */
    @Override
    public BirthdayVip GetStoreValueVip(String token, String empId, String type, int page) {
        Info info = GetInfoByToken(token);
        GetStoredValueVip getStoredValueVip = new GetStoredValueVip(info.getUrl(),info.getAccount(),info.getPassword());
        GetStoredValueVipCount g = new GetStoredValueVipCount(info.getUrl(),info.getAccount(),info.getPassword());
        GetFeedBack getFeedBack = new GetFeedBack(info.getUrl(),info.getAccount(),info.getPassword());
        BirthdayVip birthdayVip = new BirthdayVip();
        try {
            ArrayList<Vip> vips = getStoredValueVip.getData(empId, type,page);
            for (Vip vip:vips) {
                HashMap map = getFeedBack.getData(vip.getId(),"3");
                if(map != null) {
                    vip.setFeedBack(map.get("feedBack").toString());//邀约反馈
                    vip.setTarGetDate(map.get("tarGetDate").toString());
                }
//                vip.setTarGetDate();
            }

            //待完成
            int waitCount = g.getData(empId,"1");
            //已完成
            int doneCount = g.getData(empId,"2");
            int count = waitCount + doneCount;
            //完成率
            DecimalFormat df = new DecimalFormat("###.##");
            String rate ;
            if(count == 0){
                rate = "0%";
            }else {
                String s=df.format(doneCount*100D/count);
//                System.out.println(s+"%");
                rate = s+"%";
            }
            birthdayVip.setWaitCount(waitCount+doneCount);
            birthdayVip.setDoneCount(doneCount);
            birthdayVip.setUndoneCount(waitCount);
            birthdayVip.setRate(rate);
            birthdayVip.setVips(vips);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return birthdayVip;
    }
}
